var index_setting = {
	view: {
		showLine: false,
		showIcon: false,
		selectedMulti: false,
		dblClickExpand: false,
	}, data: {
		simpleData : {
			enable : true
		}
	}, callback: {
		beforeClick: index_zTreeBeforeClick,
		onClick: index_zTreeOnClick
	}
};
/**
 * 点击菜单前先的判断，如果是父节点，就展开或折叠树，否则响应点击
 */
function index_zTreeBeforeClick(treeId, treeNode, clickFlag) {
	if(treeNode.isParent) {
		index_treeObj.expandNode(treeNode);
		return false;
	} else {
		return true;
	}
}
/**
 * 点击菜单，在tab中打开页面
 */
function index_zTreeOnClick(event, treeId, treeNode) {
	index_openTab(treeNode.name, treeNode.action, true);
}
/**
 * 打开页面
 * @param title 标题
 * @param href 地址
 * @param load 是否加载同名javascript脚本
 */
function index_openTab(title, href, load) {
	var index_tabs = $("#mainbody");
	if (index_tabs.tabs("exists", title)) {
		index_mess("已经打开", 2);
		index_tabs.tabs("select", title);
	} else {
		$.get(href + "?_=" + Math.random(), function(data) {
			index_tabs.tabs("add", {
				title: title,
				content: data,
				closable: true
			});
			if(load) {
				var url = href.split(".")[0];
				$.getScript("script/" + url.split("_")[0] + "/" + url + ".js");
			}
		});
	}
}
/**
 * 更新页面
 */
function index_updateTab(title, href, load, newtitle) {
	var index_tabs = $("#mainbody");
	if(newtitle == null)
		newtitle = title;
	if (index_tabs.tabs("exists", title)) {
		if(newtitle != title && index_tabs.tabs("exists", newtitle)) {
			index_tabs.tabs("close", newtitle);
		}
		index_tabs.tabs("select", title);
		$.get(href + "?_=" + Math.random(), function(data) {
			index_tabs.tabs('update', {
				tab: index_tabs.tabs('getTab', title),
				options: {
					title: newtitle,
					content: data
				}
			});
		});
	} else {
		index_openTab(newtitle, href, load);
	}
}
function index_closeTab(title) {
	$("#mainbody").tabs("close", title);
}



$(function() {
	loadIndex();
});

var index_panel = [];
/**
 * 首页布局
 * @param reload true表示重新加载，false表示展示
 */
function loadIndex(reload) {
	if(reload) {
		$.each(index_panel, function(k, v) {
			$('#index_pp').portal('remove', v);
		});
		index_panel = [];
	} else {
		$('#index_pp').portal({
			//指定每一列的大小，0为自动调整
			columns: [0, 0, 0],
			//设置自动调整列的最小宽度
			minwidth : 360,
		});
	}
	
	
	index_load_all_order_stat();
	
	index_load_sign_order();

	index_load_new_order_stat();
	
	tem();
}


/**
 * 所有订单统计
 */
function index_load_all_order_stat(){
	var pnl = $('<div id="index_all_order_stat" class="all_order_stat"></div>').appendTo('body');  
	pnl.panel({
	    title: '所有订单',
	    height: 538,
	    collapsible: true,
	    href: "index_order_stat.action?_=" + Math.random(),
		extractor: function(data) {
			var str = '<div  id="all_order_stat" class="all_order_stat"></div>';
			$("#index_all_order_stat").html(str);
			all_order_stat_show(data);
		}, tools:[{
			iconCls: 'icon-reload',
			handler: function() {
				$('#index_all_order_stat').panel('expand').panel('refresh');
			}
		}], onLoad: function() {
			index_mess("载入成功", 2);
		}
	});
	index_panel.push(pnl);
	$('#index_pp').portal('add', {
		panel: pnl,
		columnIndex: 0
	});
}


/**
 * 标记订单展示
 */
function index_load_sign_order() {
	var pnl = $('<div id="index_sign_order" class="index_sign_order"></div>').appendTo('body');  
	pnl.panel({
		title: '标记订单',
		height: 530,
		collapsible: true,
		href: "index_sign_order.action?_=" + Math.random(),
		extractor: function(data) {
			var str = '<table id ="sign_table" class ="sign_table" ><thead><tr><th id="sign_th" class="sign_th">订单</th><th id="sign_th" class="sign_th">所在部门</th></tr></thead>';
			str = str + '<tbody id="sign_tboby">';
			$.each(data.rows, function(k, v) {
				str = str + '<tr id="sign_tr"><td id="sign_td" class="sign_td">' + v.companyNum + '</td><td id="sign_td" class="sign_td">' + v.dep + '</td></tr>';
			});
			str = str + '</tbody></table>';
			$("#index_sign_order").html(str);
			order_sign_page();
		}, tools:[{
			iconCls: 'icon-reload',
			handler: function() {
				$('#index_sign_order').panel('expand').panel('refresh');
			}
		}], onLoad: function() {
			index_mess("载入成功", 2);
		}
	});
	index_panel.push(pnl);
	$('#index_pp').portal('add', {
		panel: pnl,
		columnIndex: 1
	});
}



/**
 * 每日新订单统计
 */
function index_load_new_order_stat() {
	var pnl = $('<div id="index_new_order_stat" class="index_new_order_stat"></div>').appendTo('body');  
	pnl.panel({
	    title: '新建订单',
	    height: 380,
	    collapsible: true,
	    href: "index_new_order_stat.action?_=" + Math.random(),
		extractor: function(data) {
			var str = '<div  id="new_order_stat" class="new_order_stat"></div>';
			$("#index_new_order_stat").html(str);
			new_order_stat_show(data);
		}, tools:[{
			iconCls: 'icon-reload',
			handler: function() {
				$('#index_new_order_stat').panel('expand').panel('refresh');
			}
		}], onLoad: function() {
			index_mess("载入成功", 2);
		}
	});
	index_panel.push(pnl);
	$('#index_pp').portal('add', {
		panel: pnl,
		columnIndex: 2
	});
}

//空白
function tem(){
	var pnl = $('<div class="index_tem"></div>').appendTo('body');  
	pnl.panel({
		title: '窗口',
		height: 140,
		collapsible: true,
		content:
			'<div>敬请期待。。。</div>'
	});
	index_panel.push(pnl);
	$('#index_pp').portal('add', {
		panel: pnl,
		columnIndex: 2
	});
}



//绘制全部订单统计图
function all_order_stat_show(data) {
		var colorArray = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"];
		var myData=eval(data.rows);
		var myChart = new JSChart('all_order_stat', 'bar','');
		var colors = [];
		$.each(myData, function() {
			var colorTag = "#";
			for (var i = 0; i<6; i++){
				colorTag+=colorArray [Math.round(Math.random()*15)];
			}
			colors.push(colorTag);
		});
		
		myChart.setDataArray(myData);
		myChart.colorize(colors);
		myChart.setTitle('');
		myChart.setIntervalEndY(200);
		myChart.setGrid(false);
		myChart.setBarSpacingRatio(0);
		myChart.setBarValuesColor('#128300');
		myChart.setAxisWidth(1);
		myChart.setAxisColor('#66ACD1');
		myChart.setAxisValuesColor('#206183');
		myChart.setAxisNameColor('#206183');
		myChart.setAxisNameX(data.x);
		myChart.setAxisNameY('单数');
		myChart.setAxisReversed(true);
		myChart.set3D(true);
		myChart.setBarOpacity(0.51);
		myChart.setAxisPaddingLeft(50);
		myChart.setBarBorderColor('#FFFFFF');
		myChart.setSize(321,510);
		myChart.draw();
		
}

//绘制新建订单统计图
function new_order_stat_show(data) {
	var myChart = new JSChart('new_order_stat', 'line', '');
	var myData=eval(data.rows);
	var myData2=eval(data.rows2);
	
	$.each(myData, function(i,v) {
		myChart.setLabelX([parseInt(v[0]),v[1]]);
	});
	var arr=new Array();
	$.each(myData2, function(i,v) {
		myChart.setTooltip([parseInt(v[0]),v[1]]);
		arr.push([parseInt(v[0]),parseInt(v[1])]);
	});
	myChart.setDataArray(arr);
	myChart.setSize(350, 300);
	myChart.setIntervalEndY(40);
	myChart.setIntervalStartY(0);
	myChart.setAxisValuesNumberY(5);
	myChart.setTitle('');
	myChart.setAxisValuesColor('#444444');
	myChart.setShowXValues(false);
	myChart.setAxisNameX('日期');
	myChart.setAxisNameY('单数');
	myChart.setLineColor('#D92323');

	myChart.setFlagRadius(4);
	myChart.draw();
}


//表格分页
function order_sign_page(){
	var $alltr = $('#sign_tboby #sign_tr');
    var totalRows = $alltr.length; // 行（记录）总数
    var perPage = 25;// 每页显示行数
    var totalPageNum = Math.ceil(totalRows/perPage); //总页数
    var pageDiv = $("<div id=\"pages\" class=\"pages\"></div>");//构建分页部分

    for(var i=0;i<totalPageNum;i++){
        $('<span id="page" class="page">'+(i+1)+'</span>').appendTo(pageDiv);
    }
    pageDiv.insertAfter('#sign_table');
    
    $('.page').hover(function(){
        $(this).addClass('hover');
    }, function(){
        $(this).removeClass('hover');
    }); 
    //初始隐藏全部tr记录，只显示第一页tr记录
    $alltr.hide();
    $('.page').eq(0).addClass('currentPage');
    for(var i=0;i<perPage;i++){
        $alltr.eq(i).show();
    }
    
    $('.page').click(function(){
        $alltr.hide();//隐藏全部tr记录
        var currentPageNum = $(this).text();//当前页码
        var start = (currentPageNum-1)*perPage;//tr记录起始索引
        var end = currentPageNum*perPage-1;//tr记录截止索引
        for(var i=start;i<=end;i++){
            $alltr.eq(i).show();
        }                
    });
}
